package com.flyco.modules.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.modules.oms.entity.OmsOrderItem;
import com.flyco.modules.pms.entity.PmsProduct;
import com.flyco.modules.pms.entity.PmsSkuStock;
import com.flyco.modules.pms.mapper.PmsSkuStockMapper;
import com.flyco.modules.pms.service.IPmsProductService;
import com.flyco.modules.pms.service.IPmsSkuStockService;
import com.flyco.modules.stm.entity.StmStock;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: SKU
 * @Author: flyco
 * @Date: 2019-11-22
 * @Version: V1.0
 */
@Service
public class PmsSkuStockServiceImpl extends ServiceImpl<PmsSkuStockMapper, PmsSkuStock> implements IPmsSkuStockService {

    @Autowired
    private PmsSkuStockMapper pmsSkuStockMapper;
    @Autowired
    private IPmsProductService pmsProductService;

    @Override
    public List<PmsSkuStock> selectByMainId(Long mainId) {
        return pmsSkuStockMapper.selectByMainId(mainId);
    }


    @Override
    public int releaseSkuStockLock(List<OmsOrderItem> orderItemList) {
        return pmsSkuStockMapper.releaseSkuStockLock(orderItemList);
    }

    /**
     * 根据产品Id查询库存。
     *
     * @param productId
     * @return
     */
    @Override
    public Integer getStockByProductId(Long productId) {
        LambdaQueryWrapper<PmsSkuStock> query = new LambdaQueryWrapper<>();
        query.eq(PmsSkuStock::getProductId, productId);
        query.eq(PmsSkuStock::getStoreId, 0);
        PmsSkuStock stockInfo = getOne(query);
        return stockInfo == null ? 0 : stockInfo.getStock() - stockInfo.getLockStock();
    }

    @Override
    public int releaseLockStock(Long stockId, Integer quantity, Long version) {

        return pmsSkuStockMapper.releaseLockStock(stockId, quantity, version);
    }

    @Override
    public int releaseSkuStockLockByList(List<OmsOrderItem> orderItemList) {
        return pmsSkuStockMapper.releaseSkuStockLockByList(orderItemList);
    }

    @Override
    public boolean addByProductSnAndStoreId(String productSn, Long storeId, Integer channelId) {

        //1 判断经销商在该渠道下是否有该商品，如果有，则忽略，没有，则插入该商品
        PmsSkuStock skuStock = getByChannelIdAndProductSnAndStoreId(productSn, storeId, channelId);
        if (skuStock != null) {
            //判断状态，如果状态是删除状态，需要把状态变成可用
            if (skuStock.getCancelFlag().equals(1)) {
                updateCancelFlag(productSn,channelId,0);
            }
            return true;
        }
        PmsProduct productInfo = pmsProductService.getByProductSn(productSn);
        PmsSkuStock pmsSkuStock = new PmsSkuStock();
        pmsSkuStock.setStoreId(storeId);
        pmsSkuStock.setProductSn(productSn);
        pmsSkuStock.setChannelId(channelId);
        pmsSkuStock.setStock(0);
        pmsSkuStock.setSale(0);
        pmsSkuStock.setLockStock(0);
        pmsSkuStock.setProductId(productInfo.getId());
        pmsSkuStock.setSkuCode(productInfo.getSkuCode());

        return save(pmsSkuStock);
    }


    public PmsSkuStock getByChannelIdAndProductSnAndStoreId(String productSn, Long storeId, Integer channelId) {

        LambdaQueryWrapper<PmsSkuStock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsSkuStock::getStoreId, storeId)
                .eq(PmsSkuStock::getProductSn, productSn)
                .eq(PmsSkuStock::getChannelId, channelId);
        return getOne(queryWrapper);
    }

    /**
     * 把渠道下的某个商品标记删除
     */
    public boolean updateCancelFlag(String productSn, Integer channelId, Integer status) {

        LambdaQueryWrapper<PmsSkuStock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsSkuStock::getProductSn, productSn)
                .eq(PmsSkuStock::getChannelId, channelId);

        PmsSkuStock stock = new PmsSkuStock();
        stock.setCancelFlag(status);
        return update(stock, queryWrapper);
    }

}
